import React from "react";
import {Button, InputNumber, Modal, Select} from "antd";
import log from "../../../service/log";
import Info from "../../../service/info";
import UserModel from "../../../model/user";

export const couponOption = {
    '-1': '金币',
    // '1': 'UFO',
    // '2': '冲天火箭',
    // '3': '超级跑车',
    // '4': '双彩虹',
    // '5': '82年雪碧',
    // '6': '玫瑰花',
    // '7': '棒棒糖',
    // '8': '赞',
    '10001': '预测免费卷',
    '20001': '免费改名',
    '30001': '免费房卷',
}

const {Option,} = Select

class AddCouponModal extends React.Component {
    constructor(props) {
        super(props)
        this.model = new UserModel()
        this.state = {

            coupon: null,
            couponCount: 0,
        }
    }
    onCouponSelectChange = value => {
        log(value)
        this.setState({coupon: value,})
    }

    addCoupon = () => {
        const uid = this.props.user.Id
        const propid = this.state.coupon.value
        const propnum = this.state.couponCount
        this.model
            .addCoupon({uid, propid, propnum,})
            .then(resp => {
                const r = resp.data.ok
                if (r === 1) {
                    Info.success('添加成功')
                } else {
                    Info.error('添加失败')
                }
            })
            .then(() => {
                this.props.fetchAll()
            })
    }


    render() {
        const addCouponModal = <Modal
            title="添加道具"
            visible={this.props.visible}
            footer={null}
            maskClosable={true}
            onCancel={this.props.onCancel}
        >
            <Select
                labelInValue
                style={{width: '100%',}}
                placeholder="选择道具"
                onChange={this.onCouponSelectChange}
            >
                {Object.keys(couponOption).map((c, index) => <Option
                    value={Number(c)}
                    key={index}
                >{couponOption[c]}</Option>)}

            </Select>
            <InputNumber
                style={{marginTop: 8, marginRight: 8, width: 150,}}
                placeholder='道具数量'
                onChange={e => this.setState({couponCount: e,})}
                value={this.state.couponCount}
            />

            <Button onClick={this.addCoupon}>确认添加</Button>

        </Modal>

        return addCouponModal
    }
}

export default AddCouponModal